# -*- coding: utf-8 -*-
# @FileName : WebaseService.py
# @Time     : 2023/6/8 19:26
# @Author   : Zhong Runke
import requests, json
from application import app
import ast

class WebaseService():

    @staticmethod
    def commonReq(userAddress, funcName, funcParam, ABI, contractName, contractAddress):
        abiJSON = json.loads(ABI)
        params = {
            "groupId": "1",
            "user": userAddress,
            "contractName": contractName,
            "version": "2.8.0",
            "funcName": funcName,
            "funcParam": funcParam,
            "contractAddress": contractAddress,
            "contractAbi": abiJSON,
            "useAes": False,
            "useCna": False,
            "cnsName": ""
        }
        data = json.dumps(params)
        print("webase请求参数：", data)
        r = requests.post("http://120.25.1.147:5002/WeBASE-Front/trans/handle", data=data, headers={'Content-Type':'application/json'})
        print("webase返回结果：", r.text)
        return r.text

    @staticmethod
    def createUser(name=None):
        # http://120.25.1.147:5002/WeBASE-Front/privateKey?useAes=false&type=0&userName=202306082114
        if name is None:
            print("not name")
            return False
        params = {
            "useAes": "false",
            "type": 0,
            "userName": name
        }
        try:
            r = requests.get("http://120.25.1.147:5002/WeBASE-Front/privateKey", params=params)
        except Exception as e:
            print(e)
            return False
        print(r.text)
        r_json = json.loads(r.text)
        if 'code' in r_json:
            if r_json['code'] == 201038:
                return False
        return r_json


if __name__ == '__main__':
    # userAddress = "0x126217c80c7cd6f00b8f39e13d8b461ecf78b89d"
    # funcName = "register"
    # funcParam = ["202103130460", "钟润柯", "123456", "1755115828@qq.com", "13824555872"]
    # ABI = '[{"constant":false,"inputs":[{"name":"_newOwner","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_phone","type":"string"}],"name":"modifyPhone","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_desc","type":"string"},{"name":"_image","type":"string"},{"name":"_price","type":"uint256"},{"name":"_classifyId","type":"uint256"}],"name":"createGoods","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_orderId","type":"uint256"}],"name":"cancelOrder","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"},{"name":"_name","type":"string"},{"name":"_desc","type":"string"},{"name":"_image","type":"string"}],"name":"updateGoodsInfo","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_no","type":"string"},{"name":"_password","type":"string"}],"name":"login","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_no","type":"string"},{"name":"_name","type":"string"},{"name":"_password","type":"string"},{"name":"_email","type":"string"},{"name":"_phone","type":"string"}],"name":"register","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getUserInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_id","type":"uint256"}],"name":"getGoodsInfo","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"createWallet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_oldPassword","type":"string"},{"name":"_newPassword","type":"string"}],"name":"modifyPwd","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"},{"name":"_classifyId","type":"uint256"}],"name":"updateClassify","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_orderId","type":"uint256"}],"name":"confirmReceipt","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"},{"name":"_price","type":"uint256"}],"name":"updateGoodsPrice","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_number","type":"string"},{"name":"_orderId","type":"uint256"}],"name":"pay","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_orderId","type":"uint256"}],"name":"logistics","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"users","outputs":[{"name":"no","type":"string"},{"name":"name","type":"string"},{"name":"password","type":"string"},{"name":"email","type":"string"},{"name":"phone","type":"string"},{"name":"exists","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"rechargeWallet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_no","type":"string"}],"name":"modifySno","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"withdrawWallet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"}],"name":"removeGoods","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getWalletInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_email","type":"string"}],"name":"modifyEmail","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_orderId","type":"uint256"}],"name":"getOrderInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_orderId","type":"uint256"}],"name":"cancelOrderBackGoods","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"modifiyName","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_goodsNumber","type":"uint256"}],"name":"createOrder","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"sellerAddress","type":"address"},{"indexed":true,"name":"buyerAddress","type":"address"},{"indexed":false,"name":"goodsNumber","type":"uint256"},{"indexed":false,"name":"price","type":"uint256"}],"name":"NewOrder","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"sellerAddress","type":"address"},{"indexed":true,"name":"buyerAddress","type":"address"},{"indexed":false,"name":"number","type":"string"}],"name":"PayOrder","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"sellerAddress","type":"address"},{"indexed":false,"name":"order_id","type":"uint256"},{"indexed":false,"name":"status","type":"uint256"}],"name":"logisticsLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyerAddress","type":"address"},{"indexed":false,"name":"order_id","type":"uint256"},{"indexed":false,"name":"status","type":"uint256"}],"name":"confirmReceiptLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyerAddress","type":"address"},{"indexed":false,"name":"order_id","type":"uint256"},{"indexed":false,"name":"status","type":"uint256"}],"name":"cancelOrderLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyerAddress","type":"address"},{"indexed":false,"name":"order_id","type":"uint256"},{"indexed":false,"name":"status","type":"uint256"}],"name":"cancelOrderBackGoodsLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"name","type":"string"},{"indexed":false,"name":"price","type":"uint256"}],"name":"newGoods","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"name","type":"string"},{"indexed":false,"name":"desc","type":"string"},{"indexed":false,"name":"image","type":"string"}],"name":"updateGoodsInfoLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"price","type":"uint256"}],"name":"updateGoodsPriceLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"classifyId","type":"uint256"}],"name":"updateClassifyLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"id","type":"uint256"}],"name":"removeGoodsLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"walletAddress","type":"address"},{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"name","type":"string"},{"indexed":false,"name":"timestamp","type":"uint256"}],"name":"WalletCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"WalletTransfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"walletAddress","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"WalletRecharge","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"walletAddress","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"WalletWithdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"walletAddress","type":"address"},{"indexed":false,"name":"name","type":"string"}],"name":"WalletNameRevise","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"no","type":"string"},{"indexed":false,"name":"name","type":"string"},{"indexed":false,"name":"email","type":"string"},{"indexed":false,"name":"phone","type":"string"}],"name":"NewUserRegistered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"}],"name":"UserLoginLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"userAddress","type":"address"},{"indexed":false,"name":"_oldPassword","type":"string"},{"indexed":false,"name":"_newPassword","type":"string"}],"name":"ModifyPassWordLog","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"shipSetOwner","type":"event"}]'
    # contractName = "YiBangBang"
    # contractAddress = "0xf425216308999fba32f1fb1a44697f0c82d607eb"
    # # WebaseService.commonReq(userAddress, funcName, funcParam, ABI, contractName, contractAddress)
    # WebaseService.commonReq(userAddress, "getUserInfo", [], ABI, contractName, contractAddress)

    res = WebaseService.createUser("oesTl5ZSMGOOY6lvlZijsxmPtowU2")
    print(res)
    print(type(res))